// pages/Activity/activity.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active_index:1,
    activ_name:'',
    array_area: [],
    array_type: [
      { id: 1, name: '全部类型'},
      { id: 2, name: '巡防' },
      { id: 3, name: '信息' },
      { id: 4, name: '宣传' },
      { id: 5, name: '调解' },
      { id: 6, name: '其他' }
    ],
    array_status: [
      {id: '1',name: '全部状态'},
      {id: '2', name: '未开始'},
      {id: '3', name: '进行中'},
      ],
    index_area: 0,
    index_type: 0,
    index_status: 0,
    keywords:'',
    regionId:'',
    type:'',
    page: 1,
    allPage: '',
    longitude: 113.324520,
    latitude: 23.099994,
    markers: [],
    markers1:[],
    covers: [],
    ditufalse:false,
    ditutrue:true,
    num:2,
    array_rengwu:[]
  },

  tabItemShow:function(e){
    let that = this;
    let index = e.currentTarget.dataset.id;
    that.setData({
      active_index : index
    });
  },
  toActivityDetail:function (e){
    wx.setStorageSync("taskId", e.currentTarget.id)
    wx.navigateTo({
      url: '/pages/ActivityDetail/ActivityDetail'
    })
  },

  bindPickerChangeArea: function (e) {
    let that = this;
    let array_index = e.detail.value;
    that.setData({
      index_area: e.detail.value
    }); 
    if (that.data.array_type[that.data.index_type].name=="全部类型"){
      var leixing = "";
    }else{
      var leixing = that.data.array_type[that.data.index_type].name;
    }
    that.chaDituActive(that.data.array_area[that.data.index_area].id, leixing)
  },
  //查询地图上的活动
  chaDituActive: function (regionId, type){
    var that=this;
    if (that.data.active_index==1){
      var data = {
        paraUrl: 'task/page?activity=true&keywords=' + that.data.keywords + '&regionId=' + regionId + '&type=' + type + '&statusCode&page=1&size=1000',
        paraData: ''
      }
    }else{
      var data = {
        paraUrl: 'task/page?activity=false&keywords=' + that.data.keywords + '&regionId=' + regionId + '&type=' + type + '&statusCode&page=1&size=1000',
      }
    }
    app.getRequest(data, function (res) {
      if (res.data.result == 200) {
        var ditu=[];
        var array_huodong = res.data.body.content
        for (var i = 0; i < array_huodong.length;i++){
          var ditu1={};
          ditu1 = {
						iconPath: '../../images/activityLocate.png',
            id: i,
            latitude: array_huodong[i].latitude,
            longitude: array_huodong[i].longitude,
            width: 50,
            height: 50
        }
          ditu.push(ditu1)
        }
        if (that.data.active_index == 1) {
          that.setData({
            markers: ditu,
            array_huodong: array_huodong,
            allPage: res.data.body.totalPages
          });
        } else {
          that.setData({
            markers1: ditu,
            array_huodong: array_huodong,
            allPage: res.data.body.totalPages
          });
        }
      }
    })
  },
  bindPickerChangeType: function (e) {
    let that = this;
    let type_index = e.detail.value;
    that.setData({
      index_type: type_index
    })
    if (that.data.array_type[that.data.index_type].name == "全部类型") {
      var leixing = "";
    } else {
      var leixing = that.data.array_type[that.data.index_type].name;
    }
    that.chaDituActive(that.data.array_area[that.data.index_area].id, leixing)
  },

  bindPickerChangeStatus:function (e){
    let that = this;
    let index = e.detail.value;
    let array_status_id = that.data.array_status[index].id;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		// 合并至页面初始化的位置统一marker
    var that = this;
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
      }
    })
		// 指定跳转tab页
    if (options.id){
      var that = this;
      that.setData({
        active_index: options.id
      })
    }
		
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (type) {
    this.getAddress();
    let that = this;
    if (type == 1) {
    } else {
      that.setData({
        page: 1
      })
    }
    that.setData({
      index_area: 0,
      index_type: 0,
      index_status: 0,
    });
    var data = {
      paraUrl: 'task/page?activity=true&keywords=' + that.data.keywords + '&regionId=' + that.data.regionId + '&type=' + that.data.type + '&statusCode&page=' + that.data.page+'&size=6',
      paraData: ''
    }
    app.getRequest(data, function (res) {
      if (res.data.result == 200) {
				if (type == 1) {
					var array_huodong = that.data.array_rengwu.concat(res.data.body.content)
				} else {
					var array_huodong = res.data.body.content
				}
				// 填充地图坐标 todo 地图的数据查询逻辑需要优化, 目前仅展示第一页数据. 后面需要修改为根据当前用户坐标查周围的活动
				var mapPoints = [];
				for (var i = 0; i < array_huodong.length; i++) {
					var point = {
						iconPath: '../../images/activityLocate.png',
						id: array_huodong[i].id,
						latitude: array_huodong[i].latitude,
						longitude: array_huodong[i].longitude,
						width: 50,
						height: 50
					};
					mapPoints.push(point);
				}
				// 增加用户定位
				wx.getLocation({
					type: "wgs84",
					success: function (res) {
						var latitude = res.latitude;
						var longitude = res.longitude;
						mapPoints.push({
							latitude: latitude,
							longitude: longitude
						});
						that.setData({
							latitude: latitude,
							longitude: longitude
						})
					}
				})
				that.setData({
          array_huodong: array_huodong,
          allPage: res.data.body.totalPages,
					markers: mapPoints
        });
      }
    })
    var data2 = {
      paraUrl: 'task/page?activity=false&keywords=' + that.data.keywords + '&regionId=' + that.data.regionId + '&type=' + that.data.type + '&statusCode&page=' + that.data.page + '&size=6',
      paraData: ''
    }
    app.getRequest(data2, function (res) {
      if (res.data.result == 200){
        if (type == 1) {
          var array_rengwu = that.data.array_rengwu.concat(res.data.body.content)
        } else {
          var array_rengwu = res.data.body.content
        }
        that.setData({
          array_rengwu: array_rengwu,
          allPage: res.data.body.totalPages
        });
      }
    })
    var data1 = {
      paraUrl: 'region/applied/default',
      paraData: ''
    }
    app.getRequest(data1, function (res) {
      if (res.data.result == 200) {
        var array_area1={
          id:'',
          name:'全部区域'
        }
        res.data.body.push(array_area1)
        that.setData({
          array_area: res.data.body ,
          index_area: res.data.body.length-1
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.page < that.data.allPage) {
      that.setData({
        page: that.data.page + 1
      })
      that.onShow(1);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //发起活动
  faqihuodong:function(e){
    if(e.currentTarget.id==1){
      wx.setStorageSync("formwhere",1)
    } else if (e.currentTarget.id == 2){
      wx.setStorageSync("formwhere",2)
    }
    wx.navigateTo({
      url: '../InitiateActivity/InitiateActivity'
    })
  },
  liebiao(){
    var that=this;
    that.setData({
      num: 1,
      ditufalse: true,
      ditutrue: false,
    })
  },
  ditu(){
    var that = this;
    that.setData({
      num: 2,
      ditutrue: true,
      ditufalse: false
    })
  },
	/**
	 * 地图图标点击跳转详情页
	 */
  markertap(e) {
		wx.setStorageSync("taskId", e.markerId)
		wx.navigateTo({
			url: '/pages/ActivityDetail/ActivityDetail'
		})
  },
  //选择获取地理位置
  getAddress: function () {
    var that = this;
    app.getPermission(that)       
  }
})